#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2021/6/17 
# @Author  : Mik

from common.http_client import ApiRequest
from common.logger import logger
from common.result_base import ResultBase


class UserMange:
    def __init__(self):
        self.request = ApiRequest()

    def get_all_user(self, **kwargs):
        """
        获取全部用户信息
        :return:  result
        """
        res = self.request.get('/users', **kwargs)
        return res

    def get_user(self, username, **kwargs):
        """
        获取单个用户信息
        :param username:  用户名
        :return: 自定义的关键字返回结果 result
        """
        res = self.request.get(f'/users/{username}', **kwargs)
        logger.info("查看单个用户 ==>> 返回结果 ==>> {}".format(res.text))
        return res

    def register(self, username, password, telephone, sex="", address="", **kwargs):
        """
        注册用户信息
        :param username: 用户名
        :param password: 密码
        :param telephone: 手机号
        :param sex: 性别
        :param address: 联系地址
        :return: 自定义的关键字返回结果 result
        """
        result = ResultBase()
        json_data = {
            "username": username,
            "password": password,
            "sex": sex,
            "telephone": telephone,
            "address": address
        }
        header = {
            "Content-Type": "application/json"
        }
        res = self.request.post('/register', json=json_data, headers=header, **kwargs)
        result.success = False
        if res.json()["code"] == 0:
            result.success = True
        else:
            result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["code"], res.json()["msg"])
        result.msg = res.json()["msg"]
        result.response = res
        logger.info("注册用户 ==>> 返回结果 ==>> {}".format(result.response.text))
        return result

    def login(self, username, password, **kwargs):
        """
        登录用户
        :param username: 用户名
        :param password: 密码
        :return: 自定义的关键字返回结果 result
        """
        result = ResultBase()
        payload = {
            "username": username,
            "password": password
        }
        header = {
            "Content-Type": "application/x-www-form-urlencoded"
        }
        res = self.request.post('/login', data=payload, headers=header, **kwargs)
        result.success = False
        if res.json()["code"] == 0:
            result.success = True
            result.token = res.json()["login_info"]["token"]
        else:
            result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["code"], res.json()["msg"])
        result.msg = res.json()["msg"]
        result.response = res
        logger.info("登录用户 ==>> 返回结果 ==>> {}".format(result.response.text))
        return result

    def update(self, user_id, admin_user, new_password, new_telephone, token, new_sex="", new_address="", **kwargs):
        """
            根据用户ID，修改用户信息
            :param id: 用户ID
            :param admin_user: 当前操作的管理员用户
            :param new_password: 新密码
            :param new_telephone: 新手机号
            :param token: 当前管理员用户的token
            :param new_sex: 新性别
            :param new_address: 新联系地址
            :return: 自定义的关键字返回结果 result
            """
        result = ResultBase()
        header = {
            "Content-Type": "application/json"
        }
        json_data = {
            "admin_user": admin_user,
            "password": new_password,
            "token": token,
            "sex": new_sex,
            "telephone": new_telephone,
            "address": new_address
        }
        res = self.request.put(f'/update/user/{user_id}', json=json_data, headers=header, **kwargs)
        result.success = False
        if res.json()["code"] == 0:
            result.success = True
        else:
            result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["code"], res.json()["msg"])
        result.msg = res.json()["msg"]
        result.response = res
        logger.info("修改用户 ==>> 返回结果 ==>> {}".format(result.response.text))
        return result

    def delete(self, username, admin_user, token, **kwargs):
        """
        根据用户名，删除用户信息
        :param username: 用户名
        :param admin_user: 当前操作的管理员用户
        :param token: 当前管理员用户的token
        :return: 自定义的关键字返回结果 result
        """
        result = ResultBase()
        json_data = {
            "admin_user": admin_user,
            "token": token,
        }
        header = {
            "Content-Type": "application/json"
        }
        res = self.request.post(f'/delete/user/{username}', json=json_data, headers=header, **kwargs)
        result.success = False
        if res.json()["code"] == 0:
            result.success = True
        else:
            result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["code"], res.json()["msg"])
        result.msg = res.json()["msg"]
        result.response = res
        logger.info("删除用户 ==>> 返回结果 ==>> {}".format(result.response.text))
        return result


request_api = UserMange()
